package cn.com.dhcc.app.pub.core.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.com.dhcc.app.core.base.InterceptorBean;
import cn.com.dhcc.app.core.interceptor.InterceptorArgs;
import cn.com.dhcc.app.pub.core.annotation.TurboMenu;

/**
 * 根据操作所属叶子菜单的code定位菜单
 * @日期：2012-12-14下午11:21:12
 * @作者：乔兵
 * @版权所有：东华软件
 * @版本：1.0
 */
@Component
public class MenuCodeInterceptor extends InterceptorBean {

	public static final String MENUCODE_KEY = "menucode_key";

	@Override
	public SIGNAL beforeControllerMethodExecute(InterceptorArgs interceptorArgs) {
		HandlerMethod handlerMethod = interceptorArgs.getHandlerMethod();
		HttpServletRequest request = interceptorArgs.getRequest();
		if (request == null || request.getMethod() == null) {
			return SIGNAL.CONTINUE;
		}
		TurboMenu turboMenuOnAction = handlerMethod.getMethodAnnotation(TurboMenu.class);
		if (turboMenuOnAction != null) {
			String menuCode = turboMenuOnAction.belongToMenu().menuCode;
			request.setAttribute(MENUCODE_KEY, menuCode);
		} else {
			TurboMenu turboMenuOnClass = handlerMethod.getBean().getClass().getAnnotation(TurboMenu.class);
			if (turboMenuOnClass != null) {
				String menuCode = turboMenuOnClass.belongToMenu().menuCode;
				request.setAttribute(MENUCODE_KEY, menuCode);
			}
		}
		return SIGNAL.CONTINUE;
	}

	@Override
	public void afterControllerMethodExecute(InterceptorArgs interceptorArgs, ModelAndView modelAndView) {
	}

	@Override
	public SIGNAL interceptorExecuteErrorSignal(Exception beforeControllerMethodExecuteException) {
		return SIGNAL.CONTINUE;
	}
}
